package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.NewsTable;
import com.ysd.entity.ProductclassifiCationTable;
import com.ysd.util.DBManager;

public class NewsTableDaoImpl {
	public List<NewsTable> getNewsTable(){
		List<NewsTable> list = new ArrayList<NewsTable>();
		Statement st = null;
		ResultSet rs = null;
		Connection conn = DBManager.grtConnection();
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from NewsTable");
			while(rs.next()) {
				int newsConsultationId = rs.getInt("newsConsultationId");
				String classification = rs.getString("classification");
				NewsTable fenlei = new NewsTable(newsConsultationId, classification);
				list.add(fenlei);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	/**
	 * 分页
	 * */
	
	public List<NewsTable> getNewsTableBypage(int page,int size){
		List<NewsTable> list = new ArrayList<NewsTable>();
		Statement st = null;
		ResultSet rs = null;
		Connection conn = DBManager.grtConnection();
		try {
			st = conn.createStatement();
			String sql="select*from NewsTable LIMIT "+(page-1)*size+" ,"+size;
			st.executeQuery(sql);
			rs =st.executeQuery(sql);
			while(rs.next()) {
				int newsConsultationId = rs.getInt("newsConsultationId");
				String classification = rs.getString("classification");
				NewsTable fenlei = new NewsTable(newsConsultationId, classification);
				list.add(fenlei);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	public int deletenewstable(int cId) {
		return DBManager.executeUpdate("DELETE from newstable where newsConsultationId="+cId);
	}
	//添加产品
		public int addnews(NewsTable pc) {
			return DBManager.executeUpdate("INSERT into NewsTable VALUES(NULL,'"+pc.getClassification()+"')");
		}
		public List<NewsTable> getnews(String name){
			List<NewsTable> pcList = new ArrayList<NewsTable>();
			Connection conn = DBManager.grtConnection();
			Statement st = null;
			ResultSet rs = null;
			try {
				st = conn.createStatement();
				rs = st.executeQuery("select * from NewsTable where classification = '"+name+"'");
				while (rs.next()) {
					int pcId = rs.getInt("newsConsultationId");
					String pcName = rs.getString("classification");
					NewsTable pc = new NewsTable(pcId,pcName);
					pcList.add(pc);
				}
				return pcList;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}finally {
				DBManager.closeAll(conn, st, rs);
			}
		}
		
		public NewsTable getnewsbyid(int cateId) {
			NewsTable pc = null;
			Connection conn = DBManager.grtConnection();
			Statement st = null;
			ResultSet rs = null;
			try {
				st = conn.createStatement();
				rs= st.executeQuery("select * from NewsTable where newsConsultationId="+cateId);
				while (rs.next()) {
					int pcId = rs.getInt(1);
					String pcName = rs.getString(2);
					pc = new NewsTable(pcId,pcName);
				}
				return pc;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}finally {
				DBManager.closeAll(conn, st, rs);
			}
		}
		public int updateNews(NewsTable pc) {
			return DBManager.executeUpdate("update newstable set classification ='"+pc.getClassification()+"' where newsConsultationId="+pc.getNewsConsultationId());
		}
}
